package cn.c4ad.voa;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.text.TextUtils;
import android.util.Base64;

public class Model {
	public static class ArticalTypeParameter {
		int type;
		int fragmentTitleResId;
		String listUrl;
		String titleRegex;
		String articalUrlHead;
		String headRegex;
		String mp3urlRegex;
		String contentStart;
		String contentEnd;
	}

	public static ArticalTypeParameter getArticalTypeParameter(int type) {
		ArticalTypeParameter parameter = new ArticalTypeParameter();
		if (type == VoaContentProvider.TYPE_SPECIAL) {
			parameter.type = VoaContentProvider.TYPE_SPECIAL;
			parameter.fragmentTitleResId = R.string.ic_special;
			parameter.listUrl = "http://www.51voa.com/VOA_Special_English";
			parameter.titleRegex = "<a href=\"/VOA_Special_English/(.*?)\" target=\"_blank\">(.*?)</a>";
			parameter.articalUrlHead = "http://www.51voa.com/VOA_Special_English/";
			parameter.headRegex = "<h1>(.*?)</h1>";
			parameter.mp3urlRegex = "<a id=\"mp3\" href=\"(.*?)\"";
			parameter.contentStart = "<div id=\"content\">";
			parameter.contentEnd = "<div id=\"Bottom_VOA\">";
		} else if (type == VoaContentProvider.TYPE_STANDARD) {
			parameter.type = VoaContentProvider.TYPE_STANDARD;
			parameter.fragmentTitleResId = R.string.ic_standard;
			parameter.listUrl = "http://www.51voa.com/VOA_Standard_English/";
			parameter.titleRegex = "<a href=\"/VOA_Standard_English/(.*?)\" target=\"_blank\">(.*?)</a>";
			parameter.articalUrlHead = "http://www.51voa.com/VOA_Standard_English/";
			parameter.headRegex = "<h1>(.*?)</h1>";
			parameter.mp3urlRegex = "<a id=\"mp3\" href=\"(.*?)\"";
			parameter.contentStart = "<div id=\"content\">";
			parameter.contentEnd = "<div id=\"Bottom_VOA\">";
		}
		return parameter;
	}

	public static String getHttpString(String url) {
		String result = null;
		try {
			HttpGet httpGet = new HttpGet(url);
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(httpResponse.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static ArrayList<String[]> getArticalList(String listUrl,
			String titleRegex) {
		// listUrl="http://www.51voa.com/VOA_Special_English";
		// titleRegex="<a href=\"/VOA_Special_English/(.*?)\" target=\"_blank\">(.*?)</a>";
		ArrayList<String[]> list = new ArrayList<String[]>();
		String result = getHttpString(listUrl);
		if (!TextUtils.isEmpty(result)) {
			Pattern pattern = Pattern.compile(titleRegex);
			Matcher matcher = pattern.matcher(result);
			while (matcher.find()) {
				String url = matcher.group(1);
				String title = matcher.group(2);
				if (!TextUtils.isEmpty(url) && !TextUtils.isEmpty(title)
						&& !title.matches("<(.*?)>")) {
					String[] item = new String[2];
					item[0] = url;
					item[1] = title;
					list.add(item);
				}
			}
		}
		return list;
	}

	public static String[] getArticalContent(String articalUrl,
			String articalUrlHead, String headRegex, String mp3urlRegex,
			String contentStart, String contentEnd) {
		// articalUrl="???";
		// articalUrlHead="http://www.51voa.com/VOA_Special_English/";
		// headRegex="<h1>(.*?)</h1>";
		// mp3urlRegex="<a id=\"mp3\" href=\"(.*?)\"";
		// contentStart="<div id=\"content\">";
		// contentEnd="<div id=\"Bottom_VOA\">";
		String[] artical = new String[3];
		String result = getHttpString(articalUrlHead + articalUrl);
		if (!TextUtils.isEmpty(result)) {
			// head
			Pattern pattern = Pattern.compile(headRegex);
			Matcher matcher = pattern.matcher(result);
			if (matcher.find()) {
				artical[0] = matcher.group(1);
			}

			// mp3 url
			pattern = Pattern.compile(mp3urlRegex);
			matcher = pattern.matcher(result);
			if (matcher.find()) {
				artical[1] = matcher.group(1);
			}

			// content
			int start = result.indexOf(contentStart);
			int end = result.indexOf(contentEnd, start);
			result = result.substring(start + contentStart.length(), end);
			pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
			matcher = pattern.matcher(result);
			artical[2] = matcher.replaceAll("");
		}
		return artical;
	}

	public static String generateFileNameByUrl(String url) {
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			byte[] digestByte = messageDigest.digest(url.getBytes());
			return Base64.encodeToString(digestByte, Base64.NO_WRAP
					| Base64.URL_SAFE);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
